<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Mail;
use App\Models\Code;
use Illuminate\Support\Facades\Log;
/*use function Qiniu\json_decode;*/

class SendEmail extends Event implements ShouldBroadcast
{
    //use SerializesModels;

    public $code;
    
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Code $code)
    {
        $this->code = $code;
        $subject = '青苜绩效验证码';

        if($this->code->type == 99) {
            $subject = '【青苜网】邀请面试通知';

            $data = json_decode($this->code->content);

            $content = [
                'user'=>$data->user,
                'need'=>$data->need,
                'company'=>$data->company,
                'url'=>$data->url,
                'iuser'=>$data->iuser,
                'iphone'=>$data->iphone,
                'address'=>$data->address
            ];

            $res = Mail::send('emails.invite',$content,function($message) use($subject){
                $message->from(env('MAIL_USERNAME','master2@cimers.com'), '青苜网');
                $message ->to($this->code->phone)->subject($subject);
            });
        } else {
            if($this->code->type == 3)
            {
                $subject = '青苜网充值购买提醒';
            }

            $res = Mail::raw($this->code->content,function($message) use($subject){
                $message->from(env('MAIL_USERNAME','master2@cimers.com'), '青苜网');
                $message ->to($this->code->phone)->subject($subject);
            });
        }
        if($res)
        {
            $this->code->update(['status'=>2]);
        }
        else
        {
            $this->code->update(['status'=>3]);
            Log::info(['邮件发送失败'=>$res]);
        }
        return ['code.'.$this->code->id];
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        Log::info(['事件'=>'jijijinn']);
        $subject = '青苜网验证码';
        
        if($this->code->type == 99) {
            $subject = '【青苜网】邀请面试通知';
            
            $data = json_decode($this->code->content);
            
            $content = [
                'user'=>$data->user,
                'need'=>$data->need,
                'company'=>$data->company,
                'url'=>$data->url,
                'iuser'=>$data->iuser,
                'iphone'=>$data->iphone,
                'address'=>$data->address
            ];
            
            $res = Mail::send('emails.invite',$content,function($message) use($subject){
                $message->from(env('MAIL_USERNAME','master2@cimers.com'), '青苜网');
                $message ->to($this->code->phone)->subject($subject);
            });
        } else {
            if($this->code->type == 3)
            {
                $subject = '青苜网充值购买提醒';
            }
        
            $res = Mail::raw($this->code->content,function($message) use($subject){
                $message->from(env('MAIL_USERNAME','master2@cimers.com'), '青苜网');
                $message ->to($this->code->phone)->subject($subject);
            });
        }
        if($res)
        {
            $this->code->update(['status'=>2]);
        }
        else 
        {
            $this->code->update(['status'=>3]);
            Log::info(['邮件发送失败'=>$res]);
        }
        return ['code.'.$this->code->id];
    }
}
